package com.rtx.wms.std.transer.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.transer.bean.ApiTransferDetailFinish;
import com.rtx.wms.std.transer.bean.ApiTransferFinish;
import com.rtx.wms.std.transer.mapper.ApiTransferFinishMapper;
import com.rtx.wms.std.transer.service.ApiTransferDetailFinishService;
import com.rtx.wms.std.transer.service.ApiTransferFinishService;

@Service
public class ApiTransferFinishServiceImpl extends ServiceImpl<ApiTransferFinishMapper, ApiTransferFinish> implements ApiTransferFinishService{

	@Autowired
	ApiTransferDetailFinishService apiTransferDetailFinishService;
	
	@Transactional
	@DataSourceAnnotation
	@Override
	public void insertTransferAndDetailFinish(String datasourcekey, ApiTransferFinish finish) {
		for(ApiTransferDetailFinish td:finish.getDetails()) {
			apiTransferDetailFinishService.save(td);
		}
		this.save(finish);
	}

	@DataSourceAnnotation
	@Override
	public List<ApiTransferFinish> queryWithDetailByCondition(String datasourcekey, Wrapper<ApiTransferFinish> query) {
		List<ApiTransferFinish> list = this.list(query);
		for(ApiTransferFinish finish:list) {
			LambdaQueryWrapper<ApiTransferDetailFinish> tdqw = Wrappers.lambdaQuery();
			tdqw.eq(ApiTransferDetailFinish::getBatchid, finish.getBatchid());
			List<ApiTransferDetailFinish> detailList =  apiTransferDetailFinishService.list(tdqw);
			finish.setDetails(detailList);
		}
		return list;
	}

	@DataSourceAnnotation
	@Override
	public List<ApiTransferFinish> queryByCondition(String datasourcekey, Wrapper<ApiTransferFinish> query) {
		return this.list(query);
	}

	@DataSourceAnnotation
	@Override
	public void updStatus(String datasourcekey, Wrapper<ApiTransferFinish> upd) {
		this.update(upd);
	}
}
